package cn.gitee.ffmpeg.processed.config;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhainan
 * @date 2023/3/9 16:30
 * @region hefei
 */
@UtilityClass
public class FFmpegLocalFileContext {

    private static final ThreadLocal<Map<String, List<String>>> LOCAL = TransmittableThreadLocal.withInitial(HashMap::new);

    public static List<String> get(String key) {
        return LOCAL.get().get(key);
    }

    public static void set(String key, String value) {
        Map<String, List<String>> valueMap = LOCAL.get();
        if (valueMap.containsKey(key)) {
            valueMap.get(key).add(value);
        } else {
            valueMap.put(key, ListUtil.of(value));
        }
    }


    public static List<String> clear(String key) {
        return LOCAL.get().remove(key);
    }

    public static void clearAll() {
        LOCAL.remove();
    }
}
